%% Illustration %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Description:
%  This file is designed to explain the inputs and outputs of the main algorithm to the user using artificial data.
%  1. Generate artificial data from Nh-CES.
%  2. Calculate Money-Metics from Baqaee, Burstein and Koike-Mori (2023)'s algorithm.
%  All matrices are in (I,N,T) order: 
%  I: the number of goods, N is the number of people, and T is the number of time segments.
%
%  functions 
%   Inputs  I_vec, pvec: exogenous wage and price (I,N,T)
%   Outputs v_vec:indirect util, u_vec: Money metric , B_vec: budget shares
%   [v_vec, u_vec, B_vec] = NhCES(I_vec,pvec,paras) 

close all 
clear all
addpath('functions')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Step 1. Setting
% Generating artificial data from non-homothetic CES
% Nh-CES parameters
paras.eps_vec = [0.2;1;1.65];% Income elasticity from Comin et.al (2021)
paras.sig = 0.25;% from Comin et.al (2021)
N = 100; % number of households
T = 40;   % number of time periods
%%Setting: Price and Income
% Price Schedule
pvec(1,1,:) = reshape(linspace(1,4,T),[1,1,T]);% (I,1,T)
pvec(2,1,:) = reshape(linspace(1,3,T),[1,1,T]);% (I,1,T)
pvec(3,1,:) = reshape(linspace(1,2,T),[1,1,T]);% (I,1,T)
% Initial Income
Incometrend = reshape(linspace(1,3,T),[1,1,T]);% (I,1,T)
I_vec = linspace(1,5,N).*Incometrend;
I = size(pvec,1);paras.T = T;paras.T = T;paras.N = N;paras.I = I;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Step 1. Generate Artificial Data
% u_vec: True Money Metric (I,N,T)
% B_vec: Unconpensated Budget shares (I,N,T)
[u_vec, B_vec] = Gen_NhCES(I_vec,pvec,paras);

%% Step 2. Baqaee, Burstein and Koike-Mori (2023)'s algorithm.
%Set to 0 for iterative, 1 for recursive.
[U_vec] = CalMoneyMetric(I_vec, B_vec, pvec, 0);

%% Step 3. Plot figure
figure
loglog(I_vec(:,:,T),U_vec(:,:,T), 'LineWidth', 2)
xlabel('Income in T')
ylabel('t_0 Income that gives the same utility as in T')




